package cn.yyj.springsecurity02.config;

import cn.yyj.springsecurity02.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

@Configuration
public class MyWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsService myUserDetailService;

    @Autowired
    PersistentTokenRepository persistentTokenRepository;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();

        http.formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login")
                //.failureForwardUrl("/error1")
                .failureHandler(new MyAuthenticationFailureHandler())
                .successHandler(new MyAuthenticationSuccessHandler());
                //.successForwardUrl("/index");

        http.logout().logoutSuccessHandler(new MylogoutSuccessHandler());

        http.exceptionHandling()
                .authenticationEntryPoint(new MyAuthenticationEntryPoint())
                .accessDeniedHandler(new MyAccessDeniedHandler());

        http.authorizeRequests()
                .antMatchers("/error1","/login","/login.html","/error.html")
                .permitAll()
                .regexMatchers(".+[.]JPG").hasAnyAuthority("ROLE_ADMIN")
                .anyRequest().authenticated();


        http.rememberMe()
                .tokenValiditySeconds(30)
                .rememberMeParameter("aaa")
                .userDetailsService(myUserDetailService)
                .tokenRepository(persistentTokenRepository);
    }
}
